package com.taotao.content.service.impl;

import com.taotao.common.pojo.EasyUiTreeNode;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.content.service.ContentCategoryService;
import com.taotao.manager.mapper.TbContentCategoryMapper;
import com.taotao.manager.pojo.TbContentCategory;
import com.taotao.manager.pojo.TbContentCategoryExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {
    @Autowired
    private TbContentCategoryMapper contentCategoryMapper;
    @Override
    public List<EasyUiTreeNode> getContentCategoryList(Long parentId) {
        //注入mapper
        //创建example
        //3.设置查询条件
        //执行查询
        //转成EasyUiTreeNode
        //返回
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
        List<EasyUiTreeNode> nodes = new ArrayList<>();
        for (TbContentCategory contentCategory : list) {
            EasyUiTreeNode node = new EasyUiTreeNode();
            node.setId(contentCategory.getId());
            node.setText(contentCategory.getName());
            node.setState(contentCategory.getIsParent()?"closed":"open");
            nodes.add(node);
        }
        return nodes;
    }

    @Override
    public TaotaoResult createContentCategory(Long parentId, String name) {
        //构建对象 补全属性
        //插入contentCategory数据
        //返回的taotaoResult要包含内容分类的id
        TbContentCategory contentCategory = new TbContentCategory();
        Date date = new Date();
        contentCategory.setName(name);
        contentCategory.setParentId(parentId);
        contentCategory.setIsParent(false);
        contentCategory.setStatus(1);
        contentCategory.setSortOrder(1);
        contentCategory.setCreated(date);
        contentCategory.setUpdated(date);

        contentCategoryMapper.insertSelective(contentCategory);
        //判断父节点,如果父节点原本是叶子节点,则更新为父节点
        TbContentCategory parentContentCategory = contentCategoryMapper.selectByPrimaryKey(parentId);
        if (!parentContentCategory.getIsParent()) {
            parentContentCategory.setIsParent(true);
            contentCategoryMapper.updateByPrimaryKey(parentContentCategory);
        }
        return TaotaoResult.ok(contentCategory);
    }

    @Override
    public TaotaoResult updateContentCategory(Long id, String name) {
        TbContentCategory contentCategory = contentCategoryMapper.selectByPrimaryKey(id);
        if(contentCategory!=null){
            contentCategory.setName(name);
            contentCategory.setUpdated(new Date());
            contentCategoryMapper.updateByPrimaryKey(contentCategory);
            return TaotaoResult.ok();
        }
        return TaotaoResult.ok("要修改的记录不存在");
    }

    @Override
    public TaotaoResult deleteContentCategory(Long parentId, Long id) {
        //判断当前节点,如果是父节点,则获取所有的子节点列表
        if(isParent(id)){
            List<TbContentCategory> list = selectByParentId(id);
            //如果子节点的数量>=1,则遍历子节点列表,递归删除
            if(list!=null&&list.size()>0){
                for(TbContentCategory contentCategory:list){
                    deleteContentCategory(id,contentCategory.getId());
                }
                contentCategoryMapper.deleteByPrimaryKey(id);
            }
        }else{
            //如果当前节点是叶子节点直接删除
            contentCategoryMapper.deleteByPrimaryKey(id);
        }

        List<TbContentCategory> list1 = selectByParentId(parentId);
        if (list1 == null || list1.size() == 0) {
            TbContentCategory contentCategory = contentCategoryMapper.selectByPrimaryKey(parentId);
            contentCategory.setIsParent(false);
            contentCategoryMapper.updateByPrimaryKey(contentCategory);
        }

        return TaotaoResult.ok();
    }
    @Override
    public TaotaoResult deleteContentCategory(Long id){
        TbContentCategory contentCategory = contentCategoryMapper.selectByPrimaryKey(id);
        Long parentId=contentCategory.getParentId();
        return deleteContentCategory(parentId,id);
    }

    private boolean isParent(Long id){
        TbContentCategory contentCategory = contentCategoryMapper.selectByPrimaryKey(id);
        if(contentCategory!=null&&contentCategory.getIsParent())
            return true;
        return false;
    }

    private List<TbContentCategory> selectByParentId(Long parentId){
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
        return list;
    }
}
